#ifndef _OS_DEV_H_
#define _OS_DEV_H_

#include "types.h"
#include "dev.h"

#define MAX_DEVICES_PER_PROCESS              16



typedef enum
{
  DEVICE_STATE_READY,
  DEVICE_STATE_OPENED_READ,
  DEVICE_STATE_OPENED_READ_WRITE,
  DEVICE_STATE_BUSY,
}dev_state_t;

typedef struct
{
  dev_info_t     device_info;

  uint32_t       mode;  // mode in which the device is currently open
  dev_state_t    state; // current state of the device

  uint32_t       owner_qid;
  uint16_t       num_writers;
  uint16_t       ref_count;
}device_t;



// device-specific functions
typedef struct
{
    bool     multiple_writers;
    
    // Api to search for existing device by its name/type
    err_t (*lookup_device)(dev_info_t *device_info, device_t **device);

    // Open device
    err_t (*open_device)(dev_handle_t *handle, device_t **);

    // Close device
    err_t (*close_device)(dev_access_t *);

    // Init device
    err_t (*init_device)(void);
}device_properties_t;

typedef struct device_entry_t {

    dev_access_t          access_info;
    struct device_entry_t *next;
    struct device_entry_t *prev;
}dev_entry_t;

    

err_t init_devices(void);
void open_device(uint32_t data);
void close_device(uint32_t data);
err_t cleanup_device(dev_access_t *access_info);
void post_to_dev_owner(uint32_t data);
#endif
